<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends Controller{
	
	function toLogin(){
		$this->display("login");
	}
	function valiUser(){
		$username	=	strip_tags($_POST['username']);
		$password	=	strip_tags($_POST['password']);
		$verifyCode	=	strip_tags($_POST['verify']);
		
		//验证码校验
		$verify		=	new \Think\Verify();
		$result		=	$verify->check($verifyCode);
		if(!$result) $this->error('验证码错误!');
		
		//username password校验
		$user	=	new \Home\Common\User();
		$result	=	$user->valiUser($username,$password);
		
		if($result){
			session('mog_username',$username);
			//设置cookie
			if(I("post.remeberMe")=="yes"){
				cookie('mog_username',$username,3600*24*7);
				cookie('mog_password',$password,3600*24*7);
			}else{
				cookie('mog_username',$username,3600);
				cookie('mog_password',$password,3600);
			}
			$this->redirect("Home/Index/index");
		}else{
			$this->error("密码错误");
		}
	}
	function toLogout(){
		session('mog_username',null);
		cookie('mog_username',null);
		$this->toLogin();
	}
	function rePwd(){
		if(isset($_POST['old-pwd'])){
			$oldPwd = strip_tags($_POST['old-pwd']);
			$newPwd = strip_tags($_POST['new-pwd']);
			
			if($oldPwd != cookie('mog_password')){
				$this->error('原密码错误!');
			}else{
				$user = new \Home\Common\User();
				$status = $user->rePassword($newPwd);
				if(!status){
					$this->error("修改失败!请重试!");
				}else{
					cookie('mog_password',$newPwd,3600*24);
					$this->redirect('/Home/Index/index');
				}
			}
		}else {
			$this->display('repwd');
		}
	}
}
